#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'z417'

from webrunner.common.tools import shell
from webrunner.common import L, OSTYPE
import sys


class Environment:
    def __init__(self):
        self.pip = 'pip' if OSTYPE.startswith('win') else 'pip3'
        self.check_python()
        self.check_dependent_module()
        self.check_browser_driver_match()

    def check_python(self):
        __version__ = sys.version_info
        L.info(
            f'python Version: {__version__[0]}.{__version__[1]}.{__version__[2]}')

    def check_dependent_module(self):
        L.info('check dependent modules are installed')
        self.selenium_v()
        self.pytest_v()
        self.pytest_html_v()
        self.jinja2_v()
        self.pyyaml_v()
        self.psutil_v()
        self.click_v()

    def click_v(self):
        try:
            __v__ = shell(f'{self.pip} show Click').splitlines()[1]
            L.info(f'Click {__v__}')
        except IndexError as e:
            L.error('check Click failed:{}'.format(e))

    def psutil_v(self):
        try:
            __v__ = shell(f'{self.pip} show psutil').splitlines()[1]
            L.info('psutil {}'.format(__v__))
        except IndexError as e:
            L.error('check psutil failed:{}'.format(e))

    def pyyaml_v(self):
        try:
            __v__ = shell(f'{self.pip} show PyYAML').splitlines()[1]
            L.info('PyYAML {}'.format(__v__))
        except IndexError as e:
            L.error('check PyYAML failed:{}'.format(e))

    def jinja2_v(self):
        try:
            __v__ = shell(f'{self.pip} show Jinja2').splitlines()[1]
            L.info('Jinja2 {}'.format(__v__))
        except IndexError as e:
            L.error('check Jinja2 failed:{}'.format(e))

    def pytest_html_v(self):
        try:
            __v__ = shell(f'{self.pip} show pytest-html').splitlines()[1]
            L.info('pytest-html {}'.format(__v__))
        except IndexError as e:
            L.error('check pytest-html failed:{}'.format(e))

    def pytest_v(self):
        try:
            __v__ = shell(f'{self.pip} show pytest').splitlines()[1]
            L.info('pytest {}'.format(__v__))
        except IndexError as e:
            L.error('check pytest failed:{}'.format(e))

    def selenium_v(self):
        try:
            __v__ = shell(f'{self.pip} show selenium').splitlines()[1]
            L.info('selenium {}'.format(__v__))
        except IndexError as e:
            L.error('check selenium failed:{}'.format(e))

    def check_browser_driver_match(self):
        # TODO
        L.info('check Chrome version and chromedriver version are matched')
        self.chrome_v()
        self.browser_driver_v()

    def chrome_v(self):
        if OSTYPE == 'darwin':
            cmd = r'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version'
        elif OSTYPE.startswith('linux'):
            cmd = 'google-chrome --version || google-chrome-stable --version'
        else:
            cmd = r'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version'
        __v__ = shell(cmd)
        L.info(f'{__v__}')

    def browser_driver_v(self):
        if OSTYPE.startswith("win"):
            cmd = r'.\\driver\\chromedriver.exe --version'
        else:
            cmd = r'./driver/chromedriver --version'
        try:
            __v__ = shell(cmd)
            L.info(f'{__v__}')
        except IndexError as e:
            L.error(f'check chromedriver failed:{e}')


if __name__ == '__main__':
    Environment()
